import { merge } from '../../helper/helper';
import staticData from '../static/index';

const defaultState = {
    time: 'week',
    trigger: '', // 刷新触发器
    md_count: 4,
    items: staticData.traffic,
    salesItems: staticData.sales,
    taskItems: staticData.taskItems,
    // topList: {
    //     isPending: false,
    //     skip: 0,
    //     count: 0,
    //     chosen: [0, 1, 2, 3],
    //     data: [],
    //     currentPageIndex: 0,
    //     targetPageIndex: 0,
    //     trigger: null
    // }
    topList: [],

};

function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'set-search-time':
            return merge(state, {
                time: action.val
            })
        case 'sort-items':
            // 报表排序
            return merge(state, {
                items: action.val
            })
        case 'sort-sales-items':
            return merge(state, {
                salesItems: action.val
            })
        case 'sort-task-items':
            return merge(state, {
                taskItems: action.val
            })
        case 'change-trigger':
            return merge(state, {
                trigger: Date.now()
            })
        case 'set-traffic-loading':
            var list = { traffic: 0, total: 1, gender: 2, age: 3, sequential: 4, vipIncrease: 5, amountAndCount: 6, vipAmountAndCount: 7 };
            // switch (action.val) {
            //     case 'traffic':
            //         state.items[0].loading = true;
            //         break;
            //     case 'total':
            //         state.items[1].loading = true;
            //         break;
            //     case 'gender':
            //         state.items[2].loading = true;
            //         break;
            //     case 'age':
            //         state.items[3].loading = true;
            //         break;
            //     case 'sequential':
            //         state.items[4].loading = true;
            //         break;
            //     case 'vipIncrease':
            //         state.items[5].loading = true;
            //         break;

            // }
            state.items[list[action.val]].loading = true;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-traffic-data':
            var index = 0;
            if (state.items[0]) {
                index = state.items.findIndex(item => { return item.index == 0 });
            }
            state.items[index] = action.val;
            state.items[index].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-occupy-data':
            state.items[1] = action.val;
            state.items[1].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-gender-data':
            state.items[2] = action.val;
            state.items[2].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-age-data':
            state.items[3] = action.val;
            state.items[3].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-sequential-data':
            state.items[4] = action.val;
            state.items[4].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-vip-increase-data':
            state.items[5] = action.val;
            state.items[5].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-amount-traffic-data':
            state.items[6] = action.val;
            state.items[6].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'set-vip-amount-traffic-data':
            state.items[7] = action.val;
            state.items[7].loading = false;
            return merge(state, {
                items: [...state.items]
            })
        case 'change-time-data':
            return merge(state, {
                time: action.val
            })
        case 'top-list-set-pageIndex':
            return merge(state, {
                topList: merge(state.topList, {
                    currentPageIndex: action.val,
                    targetPageIndex: action.val
                })
            });
        case 'top-list-render-data':
            return merge(state, {
                topList: merge(state.topList, {
                    isPending: false,
                    data: action.val.data
                })
            })
        case 'top-list-request':
            return merge(state, {
                topList: merge(state.topList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'set-sales-loading':
            // switch (action.val) {
            //     case 'amount':
            //         state.salesItems[0].loading = true;
            //         break;
            //     case 'rate':
            //         state.salesItems[1].loading = true;
            //         break;
            //     case 'price':
            //         state.salesItems[2].loading = true;
            //         break;
            //     case 'complete':
            //         state.salesItems[3].loading = true;
            //         break;
            //     case 'count':
            //         state.salesItems[4].loading = true;
            //         break;
            //     case 'top':
            //         state.salesItems[5].loading = true;
            //         break;

            // }
            var list = { amount: 0, rate: 1, price: 2, complete: 3, count: 4, top: 5 };
            state.salesItems[list[action.val]].loading = true;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-layout-count':
            var list = { week: 4, month: 6, halfYear: 12 };
            // switch (action.val) {
            //     case 'week':
            //         count = 4;
            //         break;
            //     case 'month':
            //         count = 6;
            //         break;
            //     case 'halfYear':
            //         count = 12;
            //         break;
            // }
            return merge(state, {
                md_count: list[action.val]
            })
        case 'set-amount-data':
            state.salesItems[0] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-rate-data':
            state.salesItems[1] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-price-data':
            state.salesItems[2] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-complete-rate-data':
            state.salesItems[3] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-count-data':
            state.salesItems[4] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })
        case 'set-top-list-data':
            state.salesItems[5] = action.val;
            return merge(state, {
                salesItems: [...state.salesItems]
            })


    }

    return state;


}

export default reducer;